package com.spark.incoicings.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spark.incoicings.dao.OutboundDetailMapper;
import com.spark.incoicings.dao.OutboundOrderMapper;
import com.spark.incoicings.dao.ProductMapper;
import com.spark.incoicings.service.OutboundDetailService;
import com.spark.invoicings.entity.OutboundDetail;
import com.spark.invoicings.entity.OutboundOrder;
import com.spark.invoicings.entity.Product;
import com.spark.invoicings.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class OutboundDetailServiceImpl extends ServiceImpl<OutboundDetailMapper, OutboundDetail> implements OutboundDetailService {

    @Resource
    private OutboundDetailMapper outboundDetailMapper;

    @Resource
    private ProductMapper productMapper;

    @Resource
    private OutboundOrderMapper outboundOrderMapper;

    /**
     * 更新出库单明细
     * @param outboundDetail
     * @return
     */
    @Transactional
    @Override
    public Result updateOutboundDetail(OutboundDetail outboundDetail) {
        int totalQuantity = 0;
        // 出库数量
        Product product = productMapper.selectProductById(outboundDetail.getProductId());
        // 产品库存
        product.setCurrentInventory(product.getCurrentInventory() - outboundDetail.getOutboundQuantity());
        productMapper.updateById(product);
        OutboundOrder outboundOrder = outboundOrderMapper.selectOutboundOrderById(outboundDetail.getOutboundOrderId());
        List<OutboundDetail> outboundDetails = outboundDetailMapper.selectOutboundDetailByIds(outboundOrder.getOutboundId());
        outboundOrder.setOutboundDetailList(outboundDetails);
        for (OutboundDetail detail : outboundOrder.getOutboundDetailList()) {
            totalQuantity += detail.getOutboundQuantity();
        }
        // 计算出库总量
        outboundOrder.setTotalQuantity(totalQuantity);
        outboundOrderMapper.updateById(outboundOrder);
        outboundDetailMapper.updateById(outboundDetail);
        return Result.success(200, "success", outboundDetail.getOutboundDetailId());
    }

    /**
     * 保存出库单明细
     * @param outboundDetail
     * @return
     */
    @Transactional
    @Override
    public Result addOutboundDetail(OutboundDetail outboundDetail) {
        // 出库数量
        Product product = productMapper.selectProductById(outboundDetail.getProductId());
        // 产品库存
        product.setCurrentInventory(product.getCurrentInventory() - outboundDetail.getOutboundQuantity());
        productMapper.updateById(product);
        OutboundOrder outboundOrder = outboundOrderMapper.selectOutboundOrderById(outboundDetail.getOutboundOrderId());
        outboundOrder.setTotalQuantity(outboundOrder.getTotalQuantity() + outboundDetail.getOutboundQuantity());
        outboundOrderMapper.updateById(outboundOrder);
        outboundDetailMapper.insert(outboundDetail);
        return Result.success(200, "success", outboundDetail.getOutboundDetailId());
    }

    /**
     * 根据id获取出库单明细
     * @param id
     * @return
     */
    @Override
    public OutboundDetail selectOutboundDetailById(Integer id) {
        return outboundDetailMapper.selectOutboundDetailById(id);
    }

    /**
     * 查询所有出库单明细
     * @param map
     * @return
     */
    @Override
    public List<OutboundDetail> selectOutboundDetailAll(Map<String, Object> map) {
        return outboundDetailMapper.selectOutboundDetailAll(map);
    }

}
